﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Tenebrous_Manor
{
    class TextBox
    {
        Texture2D texture;
        SpriteFont font;
        String[] temp_string;
        public StringBuilder currentText = new StringBuilder("");

        Vector2 position;
        Vector2 size;// = new Vector2(400, 200);

        Vector2 textPosition;
        Rectangle r;

        int wordIndex;
        public bool triggered = false, done_printing = true, continued = false;

        Vector2 lineWidth;
        Vector2 spaceWidth;

        float padding = 25.0f;

        int BorderThickness = 1;
        Color BorderColor = Color.White;

        public TextBox(Texture2D texture,  SpriteFont font, Vector2 size)
        {
            this.texture = texture;
            this.font = font;
            this.size = size;

            triggered = false;
            lineWidth = Vector2.Zero;
            spaceWidth = font.MeasureString(" ");
        }

        public void Update(GameTime gameTime, ref Vector2 playerPosition)
        {
            TextBoxPosition(ref playerPosition);

            r = new Rectangle(
                (int)position.X,
                (int)position.Y,
                (int)size.X,
                (int)size.Y);

            textPosition = new Vector2(r.Left + padding, r.Top + padding);

            textPosition.X = (int)textPosition.X;
            textPosition.Y = (int)textPosition.Y;
            
            drawString();
        }

        public void drawString()
        {
            //if entered pressed and dialogue not all printed yet
            if (triggered && done_printing == false)
            {
                Vector2 length = font.MeasureString(temp_string[wordIndex]);

                //if text reaches the end of the line
                if (lineWidth.X + length.X + padding <= size.X - padding)
                {
                    currentText.Append(temp_string[wordIndex++] + " ");
                    lineWidth.X += length.X + spaceWidth.X;
                }
                        
                //if text reaches the end of the box height
                else if (lineWidth.Y + length.Y + padding >= size.Y )//- padding)
                {
                    if (continued == true)
                    {
                        currentText.Clear();
                        currentText.Append(temp_string[wordIndex++] + " ");
                        lineWidth.X = length.X + spaceWidth.X;
                        lineWidth.Y = length.Y;
                        continued = false;
                    }
                }
                    
                else if (lineWidth.X + length.X + padding > size.X - padding) //reaches end of line
                {
                    currentText.Append("\n" + temp_string[wordIndex++] + " ");
                    lineWidth.X = length.X + spaceWidth.X;
                    lineWidth.Y += length.Y;
                }

                if (wordIndex + 1 > temp_string.Length)
                    done_printing = true;

            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (triggered)
            {
                // Fill the button
                spriteBatch.Draw(texture, r, Color.Blue);

                // Draw the border
                spriteBatch.Draw(
                    texture,
                    new Rectangle(r.Left, r.Top, r.Width, BorderThickness),
                    BorderColor);
                spriteBatch.Draw(
                    texture,
                    new Rectangle(r.Left, r.Top, BorderThickness, r.Height),
                    BorderColor);
                spriteBatch.Draw(
                    texture,
                    new Rectangle(r.Right - BorderThickness, r.Top, BorderThickness, r.Height),
                    BorderColor);
                spriteBatch.Draw(
                    texture,
                    new Rectangle(r.Left, r.Bottom - BorderThickness, r.Width, BorderThickness),
                    BorderColor);


                // Fill the box
                //spriteBatch.Draw(texture, r, Color.Blue);

                spriteBatch.DrawString(font, currentText, textPosition, Color.White);
            }
        }

        public void TextBoxPosition(ref Vector2 playerPosition)
        {
            
            if (playerPosition.X <= 425 && playerPosition.Y <= 425) //player in top left
                position = new Vector2(playerPosition.X + 30, playerPosition.Y + 85);

            else if (playerPosition.X >= 425 && playerPosition.Y <= 425) // player in top right
                position = new Vector2(playerPosition.X - size.X + 45, playerPosition.Y + 85);

            else if (playerPosition.X <= 425 && playerPosition.Y >= 425) // player in bottom left
                position = new Vector2(playerPosition.X + 30, playerPosition.Y - size.Y - 30);

            else //player in bottom right
                position = new Vector2(playerPosition.X - size.X + 45, playerPosition.Y - size.Y - 30);
            
        }

        public void setString(String someText)
        {
            temp_string = someText.Split(' ');
            currentText.Clear();
            lineWidth.X = 0f;
            lineWidth.Y = font.MeasureString(" ").Y;
            wordIndex = 0;
            done_printing = false;
        }
    }
}
